package com.wan.data.mysql.orm.convert.auto;

import com.wan.data.mysql.orm.convert.AutoConverter;
import com.wan.data.mysql.orm.meta.ColumnMeta;
import com.wan.data.mysql.util.DbColumnType;

import java.time.Instant;

/**
 * Instant单独使用时间戳存储
 */
public class InstantConverter implements AutoConverter {
    @Override
    public Class<?>[] convertType() {
        return new Class[]{Instant.class};
    }

    @Override
    public DbColumnType getDbType() {
        return DbColumnType.BIGINT;
    }

    @Override
    public Object dbToEntity(ColumnMeta columnMeta, Object value) throws Exception {
        long time = (long) value;
        return Instant.ofEpochMilli(time);
    }

    @Override
    public Object entityToDb(ColumnMeta columnMeta, Object value) throws Exception {
        Instant instant = (Instant) value;
        return instant.toEpochMilli();
    }
}
